<%-- 
    Document   : getRoute
    Created on : Aug 10, 2009, 5:22:45 PM
    Author     : david
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"
        import="com.appspot.nnaplanner.*,
        java.util.List,
        java.util.ArrayList,
        java.util.Set,
        com.google.appengine.api.datastore.Query,
        com.google.appengine.api.datastore.DatastoreServiceFactory,
        com.appspot.nnaplanner.DAL.*,
        java.util.logging.Logger,
        java.text.DateFormat,
        java.text.SimpleDateFormat,
        java.util.Date
        "%>
<%

//test URL : http://localhost:8080/getRoute.jsp?from=s0100BRA10647&to=s0100BRP90997&time=8:00&range=20
            String from, to, time;
            int range = 0;
            Date depTime;
            DateFormat df = new SimpleDateFormat("HH:mm");
            try {
                from = request.getParameter("from");
                to = request.getParameter("to");
                time = request.getParameter("time");
                range = Integer.parseInt(request.getParameter("range"));
                depTime = df.parse(time);
            } catch (Exception ex) {
                out.print("{\"total\":\"0\", \"msg\":\"Error: Wrong parameters\"}");
                return;
            }


            //out.println("<br>" + depTime + "  " + minutes);
            List<DALConnection> route = NNappData.getRoute(from, to);
//out.println("<br>" + route.size() + "  ");
            List<DALConnection> optimumRoute = Utils.getOptimizedRoute(route, depTime, range);

            out.print("{\"total\":\"" + optimumRoute.size() + "\", \"conns\": [");
            int innercount = 0;
            int count = 0;

            for (DALConnection d : optimumRoute) {
                DALStop start = new DALStop().getByID(d.getStopID());
                DALStop end = new DALStop().getByID(d.getDestinationID());
                if (count > 0) {
                    out.print(", ");
                }
                out.print("{");
                out.print("\"conid\": \"" + d.getID() + "\", ");
                out.print("\"stopid\": \"" + d.getStopID() + "\", ");
                out.print("\"sname\": \"" + d.getStopName() + "\", ");
                out.print("\"sdesc\": \"" + start.getDescription() + "\", ");
                out.print("\"slatitude\": \"" + start.getLatitude() + "\", ");
                out.print("\"slongitude\": \"" + start.getLongitude() + "\", ");
                out.print("\"depid\": \"" + d.getDestinationID() + "\", ");
                out.print("\"dname\": \"" + d.getDestinationName() + "\", ");
                out.print("\"ddesc\": \"" + end.getDescription() + "\", ");
                out.print("\"dlatitude\": \"" + end.getLatitude() + "\", ");
                out.print("\"dlongitude\": \"" + end.getLongitude() + "\", ");
                if (d.getTimes() != null) {
                    out.print("\"departures\": \"" + d.getTimes().size() + "\", \"times\":[");
                    innercount = 0;
                    for (int i = 0; i < d.getTimes().size(); i++) {
                        if (innercount > 0) {
                            out.print(", ");
                        }
                        out.print("{");
                        out.print("\"time\": \"" + d.getTimes().get(i).toString() + "\", ");
                        out.print("\"duration\": \"" + d.getDurations().get(i).toString() + "\", ");
                        out.print("\"desc\": \"" + d.getDescriptions().get(i).toString() + "\" ");
                        out.print("}");
                        innercount++;
                    }
                    out.print("] ");

                } else {
                    out.print("\"departures\": \"0\"");
                }
                out.print("}");
                count++;
            }
            out.print("]}");

%>